<%@page import="net.groupme.user.UserManager"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>

<script type="text/javascript" language="javascript"><!--
	/**
	* Removes a Group from the Users Grouplist.
	* @param the ID of the Group to remove.
	*/
	var tmpGroupId;
	function deleteGroupMeGroup(groupId, groupName){
		tmpGroupId = groupId;
		removeCheck = confirm("Do you really want to delete the Group: " + groupName + "?");
		if (removeCheck == false) return;
		DWRUserManager.removeGroup(groupId, deleteGroupMeGroupCallback);
	}
	function deleteGroupMeGroupCallback(success) {
		if (success) {
			$('resourcepage').removeChild(dwr.util.byId("groupWithinGroupList_" + tmpGroupId));
					
			try {
 			  $('latestGroupsList').removeChild(dwr.util.byId("latestGroup_" + tmpGroupId));
			} catch (ex) {
   				
			}
			
			if($('latestGroupsList').empty()){
				$('latestGroupsList').innerHTML = "- You have no groups yet -";
			}
		}
		else {
			alert("Your session has expired.");
			window.location.reload();
			return;
		}
	}
	
	/**
	 * Leave a group as en editor. (group will not be deleted, but will no longer be listed for the user and
	 * the user will no longer be allowed to edit the group.
	 */
	function leaveGroupMeGroup(groupId, groupName){
		tmpGroupId = groupId;
		removeCheck = confirm("Do you really want to leave the group: " + groupName + "?");
		if (removeCheck == false) return;
		DWRUserManager.leaveGroup(groupId, deleteGroupMeGroupCallback);
	}
//-->
</script>


<%@page import="java.util.Iterator"%>
<%@page import="net.groupme.model.Group"%>
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="java.util.Collection"%>
<%@page import="net.groupme.model.Tag"%>
<%@page import="net.groupme.model.MODEL_CONSTANTS"%>
<%@page import="net.groupme.model.User"%>
<%@page import="org.apache.commons.lang.StringEscapeUtils"%>
									<% 
										//resultpage start
									%>
									<div id="resourcepage">
											
										
										<%
										//group output
										
										if(it.hasNext()){
											String tmpKeywordDelimeter = "";
											String keyword = null;
											
											while(it.hasNext() &&  iterStart <= iterEnd){
												
												iterStart = iterStart + 1;
												keyword = null;
												Group actualGroup = (Group) it.next();
												if(!MODEL_CONSTANTS.RESOURCE_TYPE_QUEUE.equals(actualGroup.getType())){
													
													Iterator<User> user_iter = actualGroup.getUsers("owner").iterator();
													User currentGroupUser = null;
													if (user_iter.hasNext()){
														currentGroupUser = (User) user_iter.next();
													}
													
													String thumbnail = null;
													try{
														thumbnail = actualGroup.getActualGroupVisualization(currentGroupUser.getId()).getThumbnail();
													}
													catch(Exception e){
														
													}
													//String thumbnail = actualGroup.getActualGroupVisualization(currentGroupUser.getId()).getThumbnail();
													String groupImgError = request.getContextPath() + "/images/groupimg-default.gif";
													if (thumbnail==null){
														thumbnail = actualGroup.getURLOfAPictureInGroup();
													}
													
													if (thumbnail==null & actualGroup.getURLOfAPictureInGroup()==null){
														thumbnail = request.getContextPath() + "/images/groupimg-default.gif";
													}%>
													<div class="groupWithinGroupList" id="groupWithinGroupList_<%=actualGroup.getId() %>">
														<a href="<%= VIEW_VOCABULARY.RESTful_GROUP_URI_PATTERN +  actualGroup.getId() %>"><img style="float:left; margin-right:1em; border: 0pt none" src="<%= thumbnail %>" width="120" height="80" alt="Group" onerror="this.src='<%=groupImgError %>';"/></a>
														<strong>
															<a style="clear:right" href="<%= VIEW_VOCABULARY.RESTful_GROUP_URI_PATTERN +  actualGroup.getId() %>"><%= actualGroup.getName() %></a>
														</strong>
														<% if (editGroups){ %>
														<span style="font-size: xx-small;">
															<a id="editGroup" href="<%= actualGroup.getURI() + VIEW_VOCABULARY.RESTful_EDIT_PATTERN %>" target="" title="Edit your GroupMe! Group">edit</a> 
															
															<!--  <img src="<%= request.getContextPath() %>/images/e2-48x48.gif" height="24px" width="24px" style="text-decoration:none;border:none;"/> -->
															<% 
															UserManager umA = (UserManager) WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext())
															.getBean(VIEW_VOCABULARY.USER_MANGER, UserManager.class);
																if(currentGroupUser.equals(umA.getUser())){ %>
																| <a id="deleteGroup"  href="javascript:deleteGroupMeGroup(<%=actualGroup.getId() %>, '<%= actualGroup.getName() %>')" target="" title="Delete this GroupMe! group">delete</a>
																| <a id="contributors" class="highslide" href="#" onclick="return hs.htmlExpand(this, { src: '<%= VIEW_VOCABULARY.RESTful_GROUP_URI_PATTERN + actualGroup.getId()  + "/" + VIEW_VOCABULARY.RESTful_MANAGE_CONTRIBUTORS_URI_PATTERN_PARTIAL %>', align: 'center',  objectType: 'iframe',  minWidth: 400, objectLoadTime: 'before', allowWidthReduction: 1})" title="Manage users who are allowed to edit the group.">contributors</a>
															<%}else{
																%>
															    | <a id="deleteGroup"  href="javascript:leaveGroupMeGroup(<%=actualGroup.getId() %>, '<%= actualGroup.getName() %>')" target="" title="Leave this GroupMe! group as an editor">leave</a>
															<%} %>
															<!-- <img src="<%= request.getContextPath() %>/images/delete-48x48.gif" height="24px" width="24px" style="text-decoration:none;border:none;"/> --> 
														</span>
														<%} %>
														<p> <span style="font-size:0.8em;color:gray">Created: <%= actualGroup.getDateCreated().substring(0,11) %></span></p>
														<p> <span style="font-size:0.8em;color:black">
														<% 
								
														if(actualGroup.getDescription() != null){
															
															if(actualGroup.getDescription().length() > 0){
																if(actualGroup.getDescription().length() > 80){
																	out.print("<strong>Description:&nbsp;</strong>" + actualGroup.getDescription().substring(0, 80));
																	
																}
																else if(actualGroup.getDescription().length() == 0){
																	out.print("<strong>Description:&nbsp;</strong>No description");
																}else {
																	out.print("<strong>Description:&nbsp;</strong>" + actualGroup.getDescription());
																}
																}
															else {out.print("<strong>Description:&nbsp;</strong>No description");}
															
														}else {
															out.print("<strong>Description:&nbsp;</strong>No description");
														}
														
														%>
														</span></p>
														<%
														//List tags
														Collection<Tag> tags = actualGroup.getTags(); 
														Iterator<Tag> tagIterator = tags.iterator();
														if(tagIterator.hasNext()){%>
															<small><strong>Tags:</strong>
														<%
														}
														else {%>
													    	<small><strong>Tags:</strong> no tags</small>
													    <%}
														boolean myGroupMeSearch = false;
														if(VIEW_VOCABULARY.RESTful_MyGroupMe.contains(request.getRequestURI())){
															myGroupMeSearch = true;
														}
														while(tagIterator.hasNext()){
															Tag tag = tagIterator.next();
															if(tag.getKeyword().contains(" ")){
																tmpKeywordDelimeter = "%22";
															}else{
																tmpKeywordDelimeter = "";
															}
															keyword = StringEscapeUtils.escapeHtml(tag.getKeyword());
															if(myGroupMeSearch){%>
																<a href="<%= VIEW_VOCABULARY.RESTful_MyGroupMe_TAG_SEARCH +  keyword %>" title="List your groups and resources tagged with <%= keyword %>"><%= deleteRedundantCommas(keyword) %></a><%if(tagIterator.hasNext()){%><%= ", " %><%} %>
															<%}else{%>
																<a href="<%= tag.getURI() %>" title="List groups and resources tagged with <%= keyword %>"><%= tag.getKeyword() %></a><%if(tagIterator.hasNext()){%><%= ", " %><%} %>
															<%}
														}
														if(keyword != null){%>
														</small>
														<%}%>
														</div>
														<br style="clear:left"/>
														<%
											}
										}
									}
										//resultpage end%>
										</div>
								
										